﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Yunchang;

namespace AutoChess
{
    public class PlayerInfoPanel : MonoBehaviour
    {
        public TextureKit playerIcon;
        public Text playerRunk;
        public Text curLevel;
        public Slider hpSlider;
        public Text hpText;
        public Text fightNum;
        public Text gold;

        public Text curExp;
        public Text levelUpGold;
        public Text levelUpGoldNotEnough;
        public Image levelSlider;
        public Button levelBtn;

        public Text levelUpExp;
        public Transform levelUpLableRoot;

        public UGUITweenValue tweenValue,tweenSecValue;

        float fillAmountStart = 0.139f;
        float fillAmountEnd = 0.88f;

        private int lastLevel = -1;
        private int lastExp = -1;

        private int lastFightNum = 0;
        private long lastPlayerGuid = 0;
        //进入切换调用
        public void RefreshAllPlayerInfo(ACPlayerData player)
        {
            if (lastPlayerGuid != player.GUID)//切换人了
            {
                lastLevel = -1;
                lastExp = -1;
                lastPlayerGuid = player.GUID;
            }
            levelUpExp.text = string.Format(TableTextBlo.GetText("AutoChessLevelExp"), Table.DataModel.TableConfig.autoChessUpgradeAddExp);

            levelBtn.enabled = player.IsSeeSelf;
            playerIcon.TextureName = UtilityHelper.GetShipIcon(player);
            playerRunk.text = string.Format(TableTextBlo.GetText("ChessRunk"), player.Rank);
            curLevel.text = "Lv:" + player.ShipLevel.ToString();
            RefreshFightHeroNumber(player);

            gold.text = player.Gold.ToString();
            levelUpGold.gameObject.SetActive(AutoChessManager.Ins.cacheData.CurPlayerData.Gold >= Table.DataModel.TableConfig.autoChessUpgradeCostGold);
            levelUpGoldNotEnough.gameObject.SetActive(AutoChessManager.Ins.cacheData.CurPlayerData.Gold < Table.DataModel.TableConfig.autoChessUpgradeCostGold);
            var text = AutoChessManager.Ins.cacheData.CurPlayerData.Gold >= Table.DataModel.TableConfig.autoChessUpgradeCostGold ? levelUpGold : levelUpGoldNotEnough;
            text.text = "x " + Table.DataModel.TableConfig.autoChessUpgradeCostGold;

            if (lastLevel == -1 && lastExp == -1)
            {
                lastLevel = player.ShipLevel;
                lastExp = player.ShipExp;
                SetLevelDirector(player);
                return;
            }

            if (!(lastLevel == player.ShipLevel && lastExp == player.ShipExp))//有变动
            {
                RefreshExp(player);
            }
        }

        public void RefreshPlayerHP(ACPlayerData player)
        {
            hpSlider.value = player.HP / 100f;
            hpText.text = string.Format("<color=#FFF800>{0}</color>/100", player.HP);
        }

        public void SetLevelDirector(ACPlayerData player)
        {
            var nextLevel = Table.Blo.AutoChessBlo.GetChessLevelTable(player.ShipLevel);
            if (player.ShipLevel == Table.Blo.AutoChessBlo.GetMaxChessLevel())
            {
                curExp.text = "Max";
            }
            else
            {
                curExp.text = player.ShipExp + "/" + nextLevel.upGradeExp;
            }
            levelSlider.fillAmount = player.ShipExp * 1f/nextLevel.upGradeExp; 
        }
        public void RefreshFightHeroNumber(ACPlayerData player)
        {
            if (lastFightNum < player.herosFight.Count)
            {
                UtilityHelper.PlayUIAudio("AC_addChess");
            }
            fightNum.text = (player.herosFight.Count > player.FightLimit) ?
              string.Format("<color=red>{0}</color>/{1}", player.herosFight.Count, player.FightLimit) :
               string.Format("<color=#fdec08>{0}</color>/{1}", player.herosFight.Count, player.FightLimit);
            lastFightNum = player.herosFight.Count;
        }

        public void RefreshExp(ACPlayerData player)
        {
            var maxLevel = Table.Blo.AutoChessBlo.GetMaxChessLevel();
            if (player.ShipLevel < maxLevel)
            {
                EventManager.TriggerEvent<bool>(CommonEvent.ON_SHIP_LEVEL_UPGRADES, false);
                var nextLevel = Table.Blo.AutoChessBlo.GetChessLevelTable(player.ShipLevel);
                curExp.text = player.ShipExp + "/" + nextLevel.upGradeExp;
                var percentValue = player.ShipExp * 1f / nextLevel.upGradeExp;
               
                if (player.ShipLevel == lastLevel) //通等级
                {
                    var tempvalue = levelSlider.fillAmount;
                    TweenFunction(tweenValue,tempvalue, GetExpAmountValue(percentValue),0, 0.6f, 
                    () =>
                    {
                        levelBtn.enabled = true;
                    },
                    (tween, value) =>
                    {
                        levelSlider.fillAmount = value;
                    });
                    UtilityHelper.PlayUIAudio("AC_addChessExp");
                    levelBtn.enabled = false;
                }
                else //升级
                {
                    EventManager.TriggerEvent<bool>(CommonEvent.ON_SHIP_LEVEL_UPGRADES,true);
                    CreateTweenLable();

                    var tempvalue = levelSlider.fillAmount;//升级前的值
                    var tempTarget = 1; 

                    var nowValue = 0;
                    var nowTarget = player.ShipExp * 1f / nextLevel.upGradeExp;

                    TweenFunction(tweenValue,tempvalue, tempTarget ,0, 0.3f,null,
                    (tween, value) =>
                    {
                        levelSlider.fillAmount = value;
                    });

                    TweenFunction( tweenSecValue,nowValue, GetExpAmountValue(nowTarget),0.31f, 0.3f,
                    () =>
                    {
                         levelBtn.enabled = true;
                    },
                    (tween, value) =>
                    {
                         levelSlider.fillAmount = value;
                    });
                    UtilityHelper.PlayUIAudio("AC_addChessExp");
                    levelBtn.enabled = false;
                }
            }
            else
            {
                EventManager.TriggerEvent<bool>(CommonEvent.ON_SHIP_LEVEL_UPGRADES, true);
                CreateTweenLable();

                var tempvalue = levelSlider.fillAmount;
                TweenFunction(tweenValue, tempvalue, GetExpAmountValue(1), 0, 0.6f,
                () =>
                {
                    levelBtn.enabled = true;
                    curExp.text = "Max";
                },
                (tween, value) =>
                {
                    levelSlider.fillAmount = value;
                });
                UtilityHelper.PlayUIAudio("AC_addChessExp");
                levelBtn.enabled = false;
            }

            lastLevel = player.ShipLevel;
            lastExp = player.ShipExp;
        }

        private void CreateTweenLable()
        {
            var lable = GameObject.Instantiate<GameObject>(levelUpExp.gameObject, levelUpLableRoot);
            lable.gameObject.SetActive(true);
            lable.transform.SetAsLastSibling();
            Destroy(lable, 0.4f);
        }
        private void TweenFunction(UGUITweenValue tween,float from,float to,float delateTime,float duration,UGUIEventDelegate.Callback endCall, System.Action<UGUITweenValue, float> onUpdate)
        {
            tween.ResetToBeginning();
            tween.from = from;
            tween.to = to;
            tween.delay = delateTime;
            tween.duration = duration;
            tween.onFinished.Clear();
            UGUIEventDelegate.Add(tween.onFinished, endCall);
            tween.onUpdate = onUpdate;
            tween.PlayForward();
        }

        private float GetExpAmountValue(float percentValue)
        {
            return Mathf.Lerp(fillAmountStart, fillAmountEnd, percentValue);
        }
    }
}
